<?php
/*
 * 进云智能生成平台
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
 * 小程序端，小程序端不会自动发送cookie，需要手动发送sessionID，此处访问首页，成功则返回sessionID，失败则返回0
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'login';
if(intval($_RQ['wxgid'])){
	$_SESSION['wxapp_id']=$_SESSION['appgid']=intval($_RQ['wxgid']);
}
if(!$_SESSION['wxapp_id']){
	return false;
	exi('没有传小程序id！','error');
}
if($_RQ['getmerchopenid']==1 && $_SESSION['onemerchid'] && $_SESSION['wxappopenid']){
	$jy_merch=cfc('core')->plugin_exists('jy_merch');
	if($jy_merch){
		model('jy_merch/member')->get_wxappopenid($_SESSION['onemerchid']);
	}
}
if($_SESSION['wxappopenid'] && !$_RQ['wxgid']){
	return false;
}
if(!$_SESSION['wechat_id']){
	$_SESSION['wechat_id']=pdo_getcolumn('core_account_wechat',array('uniacid'=>$_SESSION['uniacid'],'isdefault'=>1),'id');
	if(!$_SESSION['wechat_id']){
		$_SESSION['wechat_id']=pdo_getcolumn('core_account_wechat',array('uniacid'=>$_SESSION['uniacid']),'id');
	}
}
$wxapp=pdo_get('core_account_wxapp',array('id'=>$_SESSION['wxapp_id'],'uniacid'=>$_SESSION['uniacid']));
if(!$wxapp && !$_SESSION['wxappopenid']){
	return false;
	exi('指定微信小程序不存在！'.$_SESSION['wxapp_id'],'error');
}
if($_RQ['op']=='login'){
	if($_RQ['code']){
		//如果有code，根据code获取粉丝openid
		$url='https://api.weixin.qq.com/sns/jscode2session?appid='.$wxapp['appid'].'&secret='.$wxapp['appsecret'].'&js_code='.$_RQ['code'].'&grant_type=authorization_code';
		$result=cfc('http')->ihttp_get($url);
		if(is_error($result)){
			exi('获取粉丝信息失败：'.$result['message'],'error');
		}
		$result=json_decode($result['content'],true);
		if($result['openid']){
			if($_SESSION['openid'] && $_SESSION['fromh5wxapp']==1 && $_SESSION['openid']!=$result['openid']){
				//保留公众号openid
				$_SESSION['wechatopenid']=$_SESSION['openid'];
			}
			$_SESSION['openid']=$result['openid'];
			$_SESSION['wxappopenid']=$result['openid'];
			$_SESSION['session_key']=$result['session_key'];
			$mid=$_SESSION['uid'];
			if($result['unionid']){
				$result['unionid']=trim($result['unionid']);
				$_SESSION['unionid']=$result['unionid'];
				$member=pdo_get('core_members',array('unionid'=>$result['unionid'],'uniacid'=>$_SESSION['uniacid']),array('id','onemid'));
				if($member['id']){
					$mid=$member['onemid']>0?$member['onemid']:$member['id'];
				}else{
					$mid=0;
				}
			}elseif($mid){
				$member=pdo_get('core_members',array('id'=>$mid,'uniacid'=>$_SESSION['uniacid']),array('id','onemid'));
				if($member['id']){
					$mid=$member['onemid']>0?$member['onemid']:$member['id'];
				}else{
					$mid=0;
				}
			}
			$fans=pdo_get('core_fans',array('type'=>1,'openid'=>$_SESSION['openid'],'uniacid'=>$_SESSION['uniacid']),array('id','mid','nickname','avatar','sex'));
			if($fans){
				if(!$mid && $fans['mid']){
					$mid= $fans['mid'];
					$member=pdo_get('core_members',array('id'=>$mid,'uniacid'=>$_SESSION['uniacid']),array('id','onemid'));
					if($member['id']){
						$mid=$member['onemid']>0?$member['onemid']:$member['id'];
					}else{
						$mid=0;
					}
				}
			}else{
				$fansdata=array(
					'uniacid'=>$_SESSION['uniacid'],
					'wxgid'=>$_SESSION['wxapp_id'],
					'openid'=>$_SESSION['openid'],
					'type'=>1,
					'createtime' => TIMESTAMP,
				);				
				if($mid){
					$fansdata['mid']=$mid;
				}
				if($result['unionid']){
					$fansdata['unionid']=$result['unionid'];
				}
				pdo_insert('core_fans',$fansdata);
			}
			if($mid){
				model('core/member')->login($mid);
			}else{
				//自动注册会员
				$new_member=array(
					'uniacid'=>$_SESSION['uniacid'],
					'from_os'=>1,
					'createtime'=>TIMESTAMP,
					'unionid'=>$_SESSION['unionid'],
					'nickname'=>$fans['nickname']?:'',//留空好判断获取
					'avatar'=>$fans['avatar'],
					'sex'=>$fans['sex'],
					'username'=>'wxapp_'.random(10),
				);
				$uid=model('core/member')->register($new_member);
				$params=array('type'=>array(1,2,9),'openid'=>$_SESSION['openid'],'uniacid'=>$_SESSION['uniacid']);
				pdo_update('core_fans',array('mid'=>$uid),$params);
			}
		}
		$sid=session_id();
		if(!$_SESSION['base_key_str']){
			$_SESSION['base_key_str']=str_shuffle('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.-_');
		}
		$return=array('ssid'=>$sid,'session_key'=>$_SESSION['session_key'],'openid'=>$_SESSION['openid'],'uid'=>$_SESSION['uid'],'token'=>$_SESSION['token'],'base_key_str'=>$_SESSION['base_key_str']);
		$wxapp['setting']=$wxapp['setting']?json_decode($wxapp['setting'],true):array();
		//开启全局底部菜单后，是有页面菜单都以DIY的为准！
		if($wxapp['setting']['footer']['islegao_menu']==1){
			$return['menus']=$wxapp['setting']['footer']['menus'];
		}
		if((empty($return['menus']) || $wxapp['setting']['footer']['islegao_menu']!=1) && $wxapp['plugin_name'] && $wxapp['plugin_name'] !='core'){
			$plugin_name=pdo_getcolumn('core_plugins',array('name'=>$wxapp['plugin_name']),'name');
			if(!$plugin_name){
				$plugin_name=pdo_getcolumn('core_wxapp',array('name'=>$wxapp['plugin_name']),'plugin');
			}
			if($plugin_name){
				$_SESSION['current_plugin']=$plugin_name;
			}
			$file=ROOT_PLUGIN.$plugin_name.'/model/custom_mobile_menu.php';
			if(is_file($file)){
				$return['menus']=plugin($plugin_name)->custom_mobile_menu();
			}
		}
		//获取当前首页
		if($return['menus'] && $return['menus'][0]){
			if(cfc('core')->plugin_exists('jy_weishop')){
				foreach($return['menus'] as &$mv){
					if(strpos($mv['action'],'jy_weishop/member.cart')!==false){
						$mv['nums']= pdo_getsum('jy_weishop_member_cart',array('mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']),'total');
					}
				}
			}
			$_SESSION['menus_index']=$return['menus'][0]['action'];
		}
		$return['appset']=array('iscanh5'=>$wxapp['setting']['iscanh5'],'isfooter'=>$wxapp['setting']['isfooter']);
		if($_SESSION['open_jyapi']){
			$return['result']=1;
			$return['userinfo']=$fans;
			open_exi($return);
		}else{
			exi($return);
		}
	}
}
if($_RQ['op']=='sysinfo'){
	$share=array(
			'title'=>$_SESSION['account']['title'],
			'desc'=>$_SESSION['account']['desc'] ? $_SESSION['account']['desc'] : $_SESSION['account']['title'],
			'imgUrl'=>$_SESSION['account']['logo'] ? tomedia($_SESSION['account']['logo']) : SITEROOT.'core/resource/images/nopic.jpg',
	);
	if($_SESSION['open_jyapi']){
			$return['sysinfo']=$_SESSION['account'];
			$return['share']=$share;
			open_exi($return);
	}else{
			exi($return);
	}
}